package com.bawei.module_home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_home.R
import com.bawei.module_home.adapter.DeviceAdapter.DeviceViewHolder
import com.bawei.module_home.databinding.ItemDeviceBinding
import com.bawei.module_home.databinding.ItemProfilesBinding
import com.bawei.module_home.databinding.ItemVisitorBinding
import com.bawei.module_home.model.DeviceModel
import com.bawei.module_home.model.ProfilesModelItem
import com.bawei.module_home.model.VisitorListData

class VisitorAdapter : RecyclerView.Adapter<VisitorAdapter.VisitorViewHolder>() {
    val list= mutableListOf<VisitorListData>()
    fun setData(newData: List<VisitorListData>) {
        list.addAll(newData)
        notifyDataSetChanged()
    }
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): VisitorAdapter.VisitorViewHolder {
        val binding = ItemVisitorBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return VisitorViewHolder(binding)
    }

    override fun onBindViewHolder(holder: VisitorAdapter.VisitorViewHolder, position: Int) {
        holder.bind(list[position])
    }

    override fun getItemCount(): Int {
        return list.size
    }
    inner class VisitorViewHolder(private val binding: ItemVisitorBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(item: VisitorListData) {
            binding.tvDateTime.text=item.appointmentTime
            binding.tvStatus.text = item.visitStatus
            binding.tvVisitorName.text = item.visitorName
            binding.tvPhone.text = item.phoneNumber
            binding.tvReason.text = item.purpose
            binding.tvVisitTarget.text = item.plateNumber

            // 状态样式处理
            when (item.visitStatus) {
                "已通过" -> binding.tvStatus.setBackgroundResource(R.drawable.bg_status_passed)
                "待审核" -> binding.tvStatus.setBackgroundResource(R.drawable.bg_status_pending)
                "已完成" -> binding.tvStatus.setBackgroundResource(R.drawable.bg_status_completed)
            }
        }
    }
}
